/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Chat;

import Core.KeyCodeAdapter;
import Core.Value;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Mr Bao
 */
public class LoginForm extends Canvas implements Runnable {

    // <editor-fold defaultstate="collapsed" desc="Khai bao">
    private int index = 0;
    private long timer = System.currentTimeMillis();
    private int mode = 1;
    private boolean isShowPopupMenu;
    private int curPopupMenu;
    private String[] menuPopup;
    private String strUsername = "Tài khoản";
    private String strPassword = "Mật khẩu";
    private String strInvisible = "Ẩn nick";
    private String strSave = "Lưu tài khoản";
    private String strLogin = "Đăng nhập";
    private int select = 1;
    private boolean isShowPointer = true;
    private String title = "Đăng nhập";
    private String username = "baokhoai2006";
    private String password = "1091988";
    private String passMask = "*******";
    private int pointer = 0;
    private Image iconCheck;
    private Image iconUnCheck;
    private boolean isInvisible = false;
    private boolean isSave = true;
    private boolean isRunning = true;
    private int lastKey;// </editor-fold>

    public LoginForm() {
        try {
            iconCheck = Image.createImage("/Icon/check.png");
            iconUnCheck = Image.createImage("/Icon/uncheck.png");
            new Thread(this).start();
        } catch (Exception e) {
        }
    }

// <editor-fold defaultstate="collapsed" desc="Draw">
    protected void paint(Graphics g) {
        setFullScreenMode(true);
        Util.ClearScreen(g, getWidth(), getHeight());
        int y = Util.FontSmall.getHeight() * 3 / 2;
        g.setFont(Util.FontSmall);
        y += 4 * Util.FontSmall.getHeight() + 10;
        int y1 = y + Util.FontSmall.getHeight() + 5;
        int y2 = y1 + Util.FontSmall.getHeight() + 5;
        int y3 = y2 + Util.FontSmall.getHeight() + 10;
        if (y3 > getHeight() - Util.FontSmall.getHeight() * 3 / 2 - 5 && select == 5) {
            y = getHeight() - 5 * Util.FontSmall.getHeight() * 3 / 2 - Util.FontSmall.getHeight() - 10;
            DrawButton(g, y);
            y -= Util.FontSmall.getHeight() + 5;
            DrawCheckBoxSave(g, y);
            y -= Util.FontSmall.getHeight() + 5;
            DrawCheckboxInvisible(g, y);
            y -= Util.FontSmall.getHeight() + 10;
            DrawPassword(g, y);
            y -= Util.FontSmall.getHeight() + 10;
            DrawUserName(g, y);
        } else if (y2 > getHeight() - Util.FontSmall.getHeight() * 3 / 2 - 5 && select == 4) {
            y = getHeight() - 5 * Util.FontSmall.getHeight() * 3 / 2 - Util.FontSmall.getHeight() - 10;
            DrawCheckBoxSave(g, y);
            y -= Util.FontSmall.getHeight() + 5;
            DrawCheckboxInvisible(g, y);
            y -= Util.FontSmall.getHeight() + 10;
            DrawPassword(g, y);
            y -= Util.FontSmall.getHeight() + 10;
            DrawUserName(g, y);
        } else if (y1 > getHeight() - Util.FontSmall.getHeight() * 3 / 2 - 5 && select == 3) {
            y = getHeight() - 5 * Util.FontSmall.getHeight() * 3 / 2 - Util.FontSmall.getHeight() - 10;
            DrawCheckboxInvisible(g, y);
            y -= Util.FontSmall.getHeight() + 10;
            DrawPassword(g, y);
            y -= Util.FontSmall.getHeight() + 10;
            DrawUserName(g, y);
        } else {
            y = Util.FontSmall.getHeight() * 3 / 2;
            DrawUserName(g, y);
            y += 2 * Util.FontSmall.getHeight() + 10;
            DrawPassword(g, y);
            y += 2 * Util.FontSmall.getHeight() + 10;
            DrawCheckboxInvisible(g, y);
            y += Util.FontSmall.getHeight() + 5;
            DrawCheckBoxSave(g, y);
            y += Util.FontSmall.getHeight() + 10;
            DrawButton(g, y);
        }
        Util.DrawTitleBar(g, "", title, "", getWidth(), getHeight());
        Util.DrawBottomBar(g, "Menu", title, "Back", getWidth(), getHeight());
    }

    private void DrawUserName(Graphics g, int y) {
        int delta = getWidth() / 12;
        g.setColor(0);
        g.setFont(Util.FontSmall);
        g.drawString(strUsername, delta, y, 0);
        y += Util.FontSmall.getHeight();
        g.setColor(60);
        g.drawRoundRect(delta, y, getWidth() - delta * 2, Util.FontSmall.getHeight() + 5, 6, 6);
        g.setColor(200, 200, 200);
        g.drawRoundRect(delta + 1, y + 1, getWidth() - delta * 2 - 1, Util.FontSmall.getHeight() + 4, 6, 6);
        g.setColor(0xffffff);
        g.fillRoundRect(delta + 1, y + 1, getWidth() - delta * 2 - 2, Util.FontSmall.getHeight() + 3, 6, 6);
        if (select == 1 && isShowPointer) {
            g.setColor(0);
            if (Util.FontSmall.substringWidth(username, 0, pointer) > (getWidth() - delta * 2 - 1)) {
                g.drawLine(getWidth() - delta - 2, y + 2, getWidth() - delta - 2, y + Util.FontSmall.getHeight() + 2);
            } else {
                g.drawLine(delta + 2 + Util.FontSmall.substringWidth(username, 0, pointer), y + 2, delta + 2 + Util.FontSmall.substringWidth(username, 0, pointer), y + Util.FontSmall.getHeight() + 2);
            }
        }
        g.setColor(0);
        if (Util.FontSmall.stringWidth(username) > getWidth() - (delta * 2) - 1) {
            String str = "";
            int i;
            if (Util.FontSmall.substringWidth(username, 0, pointer) > getWidth() - (delta * 2) - 1) {
                i = 0;
                while (true) {
                    if (i >= 0 && i < username.length()) {
                        str = username.substring(i, pointer);
                        if (Util.FontSmall.stringWidth(str) <= getWidth() - (delta * 2) - 1) {
                            break;
                        } else {
                            i++;
                        }
                    } else {
                        break;
                    }
                }
                if (!str.equalsIgnoreCase("")) {
                    g.drawString(str, getWidth() - delta - 2, y + 3, Graphics.RIGHT | Graphics.TOP);
                }
            } else {
                i = username.length() - 1;
                while (true) {
                    if (i >= 0 && i < username.length()) {
                        str = username.substring(0, i);
                        if (Util.FontSmall.stringWidth(str) <= getWidth() - (delta * 2) - 1) {
                            break;
                        }
                        i--;
                    } else {
                        break;
                    }

                }
                if (!str.equalsIgnoreCase("")) {
                    g.drawString(str, delta + 2, y + 3, 0);
                }
            }
        } else {
            g.drawString(username, delta + 2, y + 3, 0);
        }
    }

    private void DrawPassword(Graphics g, int y) {
        int delta = getWidth() / 12;
        g.drawString(strPassword, delta, y, 0);
        y += Util.FontSmall.getHeight();
        g.setColor(60);
        g.drawRoundRect(delta, y, getWidth() - delta * 2, Util.FontSmall.getHeight() + 5, 6, 6);
        g.setColor(200, 200, 200);
        g.drawRoundRect(delta + 1, y + 1, getWidth() - delta * 2 - 1, Util.FontSmall.getHeight() + 4, 6, 6);
        g.setColor(0xffffff);
        g.fillRoundRect(delta + 1, y + 1, getWidth() - delta * 2 - 2, Util.FontSmall.getHeight() + 3, 6, 6);
        if (select == 2 && isShowPointer) {
            g.setColor(0);
            if (Util.FontSmall.substringWidth(passMask, 0, pointer) > getWidth() - delta * 2 - 1) {
                g.drawLine(getWidth() - delta - 2, y + 2, getWidth() - delta - 2, y + Util.FontSmall.getHeight() + 2);
            } else {
                g.drawLine(delta + 2 + Util.FontSmall.substringWidth(passMask, 0, pointer), y + 2, delta + 2 + Util.FontSmall.substringWidth(passMask, 0, pointer), y + Util.FontSmall.getHeight() + 2);
            }
        }
        g.setColor(0);
        if (Util.FontSmall.stringWidth(passMask) > getWidth() - (delta * 2) - 1) {
            String str = "";
            int i;
            if (Util.FontSmall.substringWidth(passMask, 0, pointer) > getWidth() - (delta * 2) - 1) {
                i = 0;
                while (true) {
                    if (i >= 0 && i < password.length()) {
                        str = password.substring(i, pointer);
                        if (Util.FontSmall.stringWidth(str) <= getWidth() - (delta * 2) - 1) {
                            break;
                        } else {
                            i++;
                        }
                    } else {
                        break;
                    }
                }
                if (!str.equalsIgnoreCase("")) {
                    g.drawString(str, getWidth() - delta - 2, y + 3, Graphics.RIGHT | Graphics.TOP);
                }
            } else {
                i = passMask.length() - 1;
                while (true) {
                    if (i >= 0 && i < passMask.length()) {
                        str = passMask.substring(0, i);
                        if (Util.FontSmall.stringWidth(str) <= getWidth() - (delta * 2) - 1) {
                            break;
                        }
                        i--;
                    } else {
                        break;
                    }
                }
                if (!str.equalsIgnoreCase("")) {
                    g.drawString(str, delta + 2, y + 3, 0);
                }
            }
        } else {
            g.drawString(passMask, delta + 2, y + 3, 0);
        }
    }

    private void DrawCheckboxInvisible(Graphics g, int y) {
        int delta = getWidth() / 12;
        g.setColor(Util.color2);
        int max = Util.FontSmall.getHeight() > iconCheck.getHeight() ? Util.FontSmall.getHeight() : iconCheck.getHeight();
        if (select == 3) {
            g.drawRoundRect(delta - 2, y - 1, Util.FontSmall.stringWidth(strInvisible) + 2 + iconCheck.getWidth() + 1, max + 3, 6, 6);
        }
        g.setColor(0);
        if (isInvisible) {
            g.drawImage(iconCheck, delta, y + (max - iconCheck.getHeight()) / 2 + 1, 0);
        } else {
            g.drawImage(iconUnCheck, delta, y + (max - iconUnCheck.getHeight()) / 2 + 1, 0);
        }
        g.drawString(strInvisible, delta + iconCheck.getWidth() + 2, y + (max - Util.FontSmall.getHeight()) / 2 + 1, 0);
    }

    private void DrawCheckBoxSave(Graphics g, int y) {
        int delta = getWidth() / 12;
        g.setColor(Util.color2);
        int max = Util.FontSmall.getHeight() > iconCheck.getHeight() ? Util.FontSmall.getHeight() : iconCheck.getHeight();
        if (select == 4) {
            g.drawRoundRect(delta - 2, y - 1, Util.FontSmall.stringWidth(strSave) + 2 + iconCheck.getWidth() + 1, max + 3, 6, 6);
        }
        g.setColor(0);
        if (isSave) {
            g.drawImage(iconCheck, delta, y + (max - iconCheck.getHeight()) / 2 + 1, 0);
        } else {
            g.drawImage(iconUnCheck, delta, y + (max - iconUnCheck.getHeight()) / 2 + 1, 0);
        }
        g.drawString(strSave, delta + iconCheck.getWidth() + 2, y + (max - Util.FontSmall.getHeight()) / 2 + 1, 0);
    }

    private void DrawButton(Graphics g, int y) {
        if (select == 5) {
            Util.GradientBox(g, Util.color1, Util.color2, (getWidth() - Util.FontSmall.stringWidth(strLogin) - 10) / 2, y, Util.FontSmall.stringWidth(strLogin) + 10, (Util.FontSmall.getHeight() + 10) / 2 + 1, 0);
            g.setColor(Util.color3);
            g.fillRect((getWidth() - Util.FontSmall.stringWidth(strLogin) - 10) / 2, y + (Util.FontSmall.getHeight() + 10) / 2, Util.FontSmall.stringWidth(strLogin) + 10, (Util.FontSmall.getHeight() + 10) / 2);
            g.setColor(Util.color2);
        } else {
            Util.GradientBox(g, Util.color1, Util.color2 - 50, (getWidth() - Util.FontSmall.stringWidth(strLogin) - 10) / 2, y, Util.FontSmall.stringWidth(strLogin) + 10, (Util.FontSmall.getHeight() + 10) / 2 + 1, 0);
            g.setColor(Util.color3);
            g.fillRect((getWidth() - Util.FontSmall.stringWidth(strLogin) - 10) / 2, y + (Util.FontSmall.getHeight() + 10) / 2, Util.FontSmall.stringWidth(strLogin) + 10, (Util.FontSmall.getHeight() + 10) / 2);
            g.setColor(150, 150, 150);
        }
        g.drawRoundRect((getWidth() - Util.FontSmall.stringWidth(strLogin) - 14) / 2, y - 1, Util.FontSmall.stringWidth(strLogin) + 13, Util.FontSmall.getHeight() + 12, 6, 6);
        g.setColor(210, 210, 210);
        g.drawString(strLogin, (getWidth() - Util.FontSmall.stringWidth(strLogin)) / 2, y + 5, 0);
    }// </editor-fold>

    public void run() {
        while (isRunning) {
            try {
                Thread.sleep(500);
                if (System.currentTimeMillis() - timer > 1000) {
                    passMask = Util.ResetPassword(passMask);
                }
                isShowPointer = !isShowPointer;
                repaint();
            } catch (Exception e) {
            }
            Runtime.getRuntime().gc();
        }
    }

    protected void keyPressed(int keyCode) {
        int instantkeyCode = KeyCodeAdapter.getInstance().adoptKeyCode(keyCode);
        ProcessKeyPress(instantkeyCode, keyCode);
    }

    private void ProcessKeyPress(int ikeyCode, int keyCode) {

        switch (ikeyCode) {
            case KeyCodeAdapter.SOFT_KEY_LEFT:
                isShowPopupMenu = !isShowPopupMenu;
                curPopupMenu = 0;
                repaint();
                return;
            case KeyCodeAdapter.DELETE_KEY:
            case KeyCodeAdapter.SOFT_KEY_RIGHT:
                if (select == 1) {
                    if (pointer == 0) {
                        username = Util.DelKey(username, pointer);

                    } else {
                        username = Util.DelKey(username, pointer);
                        pointer--;
                    }
                }
                if (select == 2) {
                    if (pointer == 0) {
                        password = Util.DelKey(password, pointer);
                        passMask = Util.DelKey(passMask, pointer);
                    } else {
                        password = Util.DelKey(password, pointer);
                        passMask = Util.DelKey(passMask, pointer);
                        pointer--;
                    }
                }
                repaint();
                return;
        }
        switch (keyCode) {
            case -1:
                if (select > 1) {
                    select--;
                    if (select == 1) {
                        pointer = username.length();
                    } else if (select == 2) {
                        pointer = password.length();
                    }
                    repaint();
                }
                lastKey = -1;
                return;
            case -2:
                if (select < 5) {
                    select++;
                    if (select == 1) {
                        pointer = username.length();
                    } else if (select == 2) {
                        pointer = password.length();
                    }
                    repaint();
                }
                lastKey = -1;
                return;
            case -3:
                if (select == 1 || select == 2) {
                    if (pointer > 0) {
                        pointer--;
                    }
                }
                repaint();
                lastKey = -1;
                return;
            case -4:
                if (select == 1) {
                    if (pointer < username.length()) {
                        pointer++;
                    }
                }
                if (select == 2) {
                    if (pointer < password.length()) {
                        pointer++;
                    }
                }
                repaint();
                lastKey = -1;
                return;
            case -5:
            case 10:
                switch (select) {
                    case 3:
                        isInvisible = !isInvisible;
                        repaint();
                        return;
                    case 4:
                        isSave = !isSave;
                        repaint();
                        return;
                    case 5:
                        BtnLoginClick();
                        return;
                }
                return;
            case -8:
                if (select == 1) {
                    if (pointer == 0) {
                        username = Util.DelKey(username, pointer);
                    } else {
                        username = Util.DelKey(username, pointer);
                        pointer--;
                    }
                }
                if (select == 2) {
                    if (pointer == 0) {
                        password = Util.DelKey(password, pointer);
                        passMask = Util.DelKey(passMask, pointer);
                    } else {
                        password = Util.DelKey(password, pointer);
                        passMask = Util.DelKey(passMask, pointer);
                        pointer--;
                    }
                }
                repaint();
                return;
            case 35:
                mode++;
                if (mode > 3) {
                    mode = 1;
                }
                return;
            default:
                if (!Util.isNumericKeyPad(this)) {
                    if (select == 1) {
                        username = Util.SelectKey(username, pointer, keyCode);
                        pointer++;
                    } else if (select == 2) {
                        password = Util.SelectKey(password, pointer, keyCode);
                        passMask += "*";
                        pointer++;
                    }
                    repaint();
                } else {
                    switch (mode) {
                        case 1:
                            if (select == 1) {
                                Value value = new Value(username, pointer, index, timer, lastKey);
                                value = Util.SelectKey(value, keyCode, Util.keyCodesLowCase);
                                username = value.string;
                                lastKey = value.lastKey;
                                index = value.index;
                                timer = value.timer;
                                pointer = value.pointer;
                            } else if (select == 2) {
                                Value value = new Value(password, passMask, pointer, index, timer, lastKey);
                                value = Util.SelectKey(value, keyCode, Util.keyCodesLowCase);
                                password = value.string;
                                lastKey = value.lastKey;
                                index = value.index;
                                timer = value.timer;
                                passMask = value.password;
                                pointer = value.pointer;
                            }
                            repaint();
                            return;
                        case 2:
                            if (select == 1) {
                                Value value = new Value(username, pointer, index, timer, lastKey);
                                value = Util.SelectKey(value, keyCode, Util.keyCodesUpCase);
                                username = value.string;
                                lastKey = value.lastKey;
                                index = value.index;
                                timer = value.timer;
                                pointer = value.pointer;
                            } else if (select == 2) {
                                Value value = new Value(password, passMask, pointer, index, timer, lastKey);
                                value = Util.SelectKey(value, keyCode, Util.keyCodesUpCase);
                                password = value.string;
                                lastKey = value.lastKey;
                                index = value.index;
                                timer = value.timer;
                                passMask = value.password;
                                pointer = value.pointer;
                            }
                            repaint();
                            return;
                        case 3:
                            if (select == 1) {
                                username = Util.SelectNum(username, pointer, keyCode);
                                pointer++;
                            } else if (select == 2) {
                                password = Util.SelectNum(password, pointer, keyCode);
                                passMask += "*";
                                pointer++;
                            }
                            repaint();
                            return;
                    }
                }
        }
    }

    private void BtnLoginClick() {
        Yahoo.DisplaySplashScreen(username, password, isInvisible);
    }
}
